package hopital.orm;

import hopital.sql.SqlObject;
import hopital.sql.SqlQuery;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Chambre extends SqlObject{
    private double _prix;
    private String _status,_nomBloc;
    private int _bloc;
    public Chambre() {
    }

    public String getNomBloc() {
        return _nomBloc;
    }

    public void setNomBloc(String _nomBloc) {
        this._nomBloc = _nomBloc;
    }

    
    
    public int getBloc() {
        return _bloc;
    }

    public void setBloc(int _bloc) {
        this._bloc = _bloc;
    }

    
    public Chambre(double _prix, String _status, int _bloc) {
        this._prix = _prix;
        this._status = _status;
        this._bloc = _bloc;
    }

    
    
    
    public  Chambre(ResultSet _rs) throws SQLException{
        this._prix = _rs.getDouble("c_prix");
        this._nomBloc =_rs.getString("c_nom");
        this._status =_rs.getString("c_status");
        this._bloc = _rs.getInt("c_bloc");
        this._id = _rs.getInt("c_id");
    }

    public double getPrix() {
        return _prix;
    }

    public void setPrix(double _prix) {
        this._prix = _prix;
    }

    public String getStatus() {
        return _status;
    }

    public void setStatus(String _status) {
        this._status = _status;
    }
    
    
    @Override
    public SqlQuery insert() {
        return new SqlQuery("INSERT INTO t_chambres (c_prix,c_bloc) VALUES (?,?);",_prix,_bloc);
    }

    @Override
    public SqlQuery update() {
        return new SqlQuery("UPDATE t_chambres SET c_prix=?,c_status=?,c_bloc=? WHERE c_id=?",_prix,_status,_bloc,_id);
    }

    @Override
    public SqlQuery delete() {
        return new SqlQuery("DELETE FROM t_chambres WHERE c_id=?",_id);
    }

    @Override
    public SqlQuery select() {
        if(_id!=-1){
            return new SqlQuery("SELECT t_chambres.*,t_blocs.c_nom FROM t_chambres JOIN t_blocs ON t_chambres.c_bloc=t_blocs.c_id WHERE c_id=?;", _id);
        }else if(_bloc!=0){ 
            return new SqlQuery("SELECT t_chambres.*,t_blocs.c_nom FROM t_chambres JOIN t_blocs ON t_chambres.c_bloc=t_blocs.c_id WHERE c_bloc=?;", _bloc);
        }else{
            return new SqlQuery("SELECT t_chambres.*,t_blocs.c_nom FROM t_chambres JOIN t_blocs ON t_chambres.c_bloc=t_blocs.c_id;");
        }
    }

    @Override
    public String toString() {
        return " N° "+ _id+" Bloc=" + _nomBloc ;
    }

    

    
    
    @Override
    public List<Object> getTable(ResultSet _rs) throws Exception {
        List<Object> list=new ArrayList<>();
        while (_rs.next()) {            
            list.add(new Chambre(_rs));
        }
        return list;
    }
}